package com.gs.cd.service.resource.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.binding.Binder;
import com.gs.cd.model.resource.SysResource;
import com.gs.cd.model.resource.SysResourceGroupOwnResource;
import com.gs.cd.model.resource.SysSale;
import com.gs.cd.service.resource.mapper.SysResourceMapper;
import com.gs.cd.service.resource.service.SysResourceGroupOwnResourceService;
import com.gs.cd.service.resource.service.SysResourceGroupService;
import com.gs.cd.service.resource.service.SysResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gs.cd.service.resource.service.SysSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author seven
 * @since 2020-12-22
 */
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements SysResourceService {


    @Autowired
    private SysSaleService sysSaleService;
    @Autowired
    private SysResourceGroupService sysResourceGroupService;
    @Autowired
    private SysResourceGroupOwnResourceService sysResourceGroupOwnResourceService;

    @Override
    public List<SysResource> listTreeBySaleCode(String saleCode) {
        SysSale sysSale = sysSaleService.getByCode(saleCode);
        return listTreeByProjectId(sysSale.getProjectId());
    }

    /**
     * 获取树
     * 排序是sort asc降序 越小越在前
     *
     * @param productId
     * @return
     */
    @Override
    public List<SysResource> listTreeByProjectId(String productId) {
        return Binder.convertAndBindRelations(list(new QueryWrapper<SysResource>().lambda()
                .eq(SysResource::getProductId, productId).isNull(SysResource::getParentId)
                .orderByAsc(SysResource::getSort)
        ), SysResource.class);
    }

    @Override
    public boolean copy(String sourceProductId, String targetProductId) {
        //1. 获取源数据库
        List<SysResource> list = listByProjectId(sourceProductId);
        //2. 删除
        boolean b = removeByProjectId(targetProductId);
        //3. 设置值
        list.forEach(s -> {
            s.setId(StrUtil.removeSuffix(s.getId(), sourceProductId) + targetProductId);
            s.setParentId(s.getParentId() == null ? null : StrUtil.removeSuffix(s.getParentId(), sourceProductId) + targetProductId);
            s.setProductId(targetProductId);
        });
        int i = getBaseMapper().insertBatchNotAuto(list);
        return i != 0;
    }

    @Override
    public boolean removeByProjectId(String productId) {
        return remove(new QueryWrapper<SysResource>().lambda().eq(SysResource::getProductId, productId));
    }

    @Override
    public List<SysResource> listByProjectId(String sourceProductId) {
        return list(new QueryWrapper<SysResource>().lambda().eq(SysResource::getProductId, sourceProductId));
    }

    @Override
    public Map relatedgroup(String id) {
        List<Map<String, String>> relatedInfo = getBaseMapper().getRelatedInfo(id);
        Map<String, List<Map<String, String>>> sale_name = relatedInfo.stream().
                collect(Collectors.groupingBy(stringStringMap -> stringStringMap.get("sale_name")));
        return sale_name;
    }

    @Override
    public boolean drag(List<String> ids) {
        Collection<SysResource> list = listByIds(ids);
        Map<String, SysResource> collect = list.stream().collect(Collectors.toMap(SysResource::getId, s -> s));
        for (int i = 0; i < ids.size(); i++) {
            collect.get(ids.get(i)).setSort(i);
        }
        return updateBatchById(collect.values());
    }

    @Override
    public List<SysResource> listOnlyRoot(String saleCode) {
        SysSale sysSale = sysSaleService.getByCode(saleCode);
        return list(new QueryWrapper<SysResource>().lambda()
                .eq(SysResource::getProductId, sysSale.getProjectId())
                .isNull(SysResource::getParentId));
    }
}
